﻿/*
 * Description: process monitor dll entry.
 */
#include <iostream>
#include <windows.h>
#include <string>
#include <vector>
#include <Psapi.h>
#include "aspect.h"

static std::wstring GetCurrentProcessName()
{
    auto process = GetCurrentProcess();
    std::vector<wchar_t> processName(MAX_PATH);
    GetProcessImageFileNameW(process, processName.data(), MAX_PATH);
    std::wstring name = processName.data();
    auto preLocation = name.rfind(L"\\");
    if (preLocation == std::wstring::npos || preLocation == name.size() - 1) {
        return L"";
    }
    return name.substr(preLocation + 1);
}

static const std::vector<std::wstring> basicRenderList = {
    L"Photos.exe",
    L"PhotosApp.exe"
};

static const std::vector<std::wstring> animationList = {
    L"WINWORD.EXE"
};

static bool IsInAppList(const std::vector<std::wstring>& appList)
{
    auto currentAppName = GetCurrentProcessName();
    for (auto& appName : appList) {
        if (currentAppName == appName) {
            return true;
        }
    }
    return false;
}

BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
    if (ul_reason_for_call == DLL_PROCESS_ATTACH) {
        if (IsInAppList(basicRenderList)) {
            SVGA::AppendAspectBasicRender();
        } else if (IsInAppList(animationList)) {
            SVGA::AppendAspectAnimation();
        }
    }
    return TRUE;
}
